//
//  ShoesCouponsSaleOrderDetailVC.swift
//  TestDemo
//
//  Created by ayin on 2019/5/10.
//  Copyright © 2019 aYin. All rights reserved.
//

import UIKit

class ShoesCouponsSaleOrderDetailVC: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        self.view.backgroundColor = UIColor.white
        
//        let lalla = ShoesCouponsSaleOrderDetailView(frame: .zero)
//        lalla.backgroundColor = UIColor.white
//        self.view.addSubview(lalla)
//
//        lalla.snp.makeConstraints { (make) in
//            make.left.right.equalToSuperview()
//            make.top.equalTo(200)
//        }
//
//        lalla.refreshDatas("kkk")
        
        let pagerView = TYCyclePagerView()
        pagerView.layer.borderWidth = 1
        pagerView.isInfiniteLoop = false
        pagerView.dataSource = self
        pagerView.delegate = self
        pagerView.register(TYCyclePageCell.self, forCellWithReuseIdentifier: TYCyclePageCell.reuseId())
        self.view.addSubview(pagerView)
        
        pagerView.snp.makeConstraints { (make) in
            make.top.equalTo(100)
            make.left.equalToSuperview()
            make.width.equalTo(screenWidth)
            make.height.equalTo(screenHeight - 100)
        }
    }

}

extension ShoesCouponsSaleOrderDetailVC: TYCyclePagerViewDataSource,TYCyclePagerViewDelegate {
    func numberOfItems(in pageView: TYCyclePagerView) -> Int {
        return 3
    }
    
    func pagerView(_ pagerView: TYCyclePagerView, cellForItemAt index: Int) -> UICollectionViewCell {
        let cell = pagerView.dequeueReusableCell(withReuseIdentifier: TYCyclePageCell.reuseId(), for: index) as! TYCyclePageCell
        cell.cellRefreshDatas(index)
        return cell
    }
    
    func layout(for pageView: TYCyclePagerView) -> TYCyclePagerViewLayout {
        let layout = TYCyclePagerViewLayout()
        layout.itemSize = CGSize(width: 325.up, height: 100)
        layout.itemSpacing = 15
        layout.minimumScale = 0.9
        layout.layoutType = .linear
        return layout
    }
    
    
    
    func pagerView(_ pageView: TYCyclePagerView, didSelectedItemCell cell: UICollectionViewCell, at index: Int) {
        print("llalalalalalla")
    }
}

class TYCyclePageCell: UICollectionViewCell {
    //MARK: - 声明属性
    var containtView: UIView = {
        let view = UIView()
        view.backgroundColor = UIColor.blue
        return view
    }()
    
    var productimageView: UIImageView = {
        let imgView = UIImageView()
        imgView.contentMode = .scaleAspectFill
        imgView.clipsToBounds = true
        imgView.backgroundColor = UIColor.red
        return imgView
    }()
    
    lazy var seeMoreBtn: UIButton = {
        let btn = UIButton.makeButton(font: UIFont.pingfangRegular(size: 12.up), title: "已发售21场  看往期", titleColor: UIColor.black, image: "hopeToBuy_aboutRegister_arrow", bgColor: UIColor.clear)
        return btn
    }()
    
    var hintInfoLabel: UILabel = {
        let label = UILabel.makeLabel(font: UIFont.pingfangRegular(size: 10.up), text: "预售券抢到可转卖，到期可提货", textColor: UIColor.black, numberLine: 1, alignment: .left)
        return label
    }()
    
    var hSpace: CGFloat = 15
    var vSpace: CGFloat = 15
    
    //MARK: - 初始化
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupSubViews()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    static func reuseId() -> String {
        return NSStringFromClass(TYCyclePageCell.self)
    }
    
    //MARK: - 搭建UI
    func setupSubViews() {
        contentView.backgroundColor = UIColor.white
        
        contentView.addSubview(containtView)
        containtView.addSubview(productimageView)
        containtView.addSubview(seeMoreBtn)
        containtView.addSubview(hintInfoLabel)
        
        containtView.snp.makeConstraints { (make) in
            make.edges.equalToSuperview()
        }
        
        productimageView.snp.makeConstraints { (make) in
            make.left.equalToSuperview().offset(hSpace)
            make.width.equalTo(80.up)
            make.height.equalTo(40.up)
            make.centerY.equalToSuperview()
        }
        
        seeMoreBtn.snp.makeConstraints { (make) in
            make.left.equalTo(productimageView.snp.right).offset(hSpace)
            make.right.lessThanOrEqualToSuperview().offset(-hSpace)
            make.top.equalTo(productimageView)
        }
        
        hintInfoLabel.snp.makeConstraints { (make) in
            make.left.equalTo(seeMoreBtn)
            make.right.lessThanOrEqualToSuperview().offset(-hSpace)
            make.bottom.equalTo(productimageView)
        }
    }
    
    //MARK: - 事件
    //刷新数据
    func cellRefreshDatas(_ count: Int) {
        seeMoreBtn.setTitle("已发售\(count)场  看往期", for: .normal)
    }
}
